package InterviewTest.q0208_detectCycle;
import CommonClass.Common.ListNode;
public class Solution {
    /*
    与142题相同
     */
    public ListNode detectCycle(ListNode head) {
        if (head == null || head.next == null) return null;

        ListNode slow = head, fast = head;
        while (fast.next != null && fast.next.next != null) {
            fast = fast.next.next;
            slow = slow.next;
            if (slow == fast) break;
        }
        if (fast.next == null || fast.next.next == null) return null;

        ListNode node = head;
        while (node != slow) {
            node = node.next;
            slow = slow.next;
        }

        return node;

    }
}
